﻿/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#ifndef _GCTL_TESSEROID_H
#define _GCTL_TESSEROID_H

#include "vertex.h"
#include "entity.h"

namespace gctl
{
	// Declaration of the basic edge type
    template <typename A> struct type_tesseroid;

    typedef type_tesseroid<void> tesseroid; // edge type of attribute type of void

	/**
	 * 
	 * @brief      structure of a regular block under the Cartesian coordinates.
	 * 
	 * Coordinate system used in the algorithm.
	 * 
	 *    lat
	 *   /      /------ur
	 *  /      /      /|
	 * O------> lon  / |
	 * |     /      / /
	 * |    |------| /
	 * |    dl------/
	 * |
	 * radius
	 * 
	 */
    template <typename A>
	struct type_tesseroid : public entity<vertex3ds, 8, A>
	{
		/**
		 * the down left and up right corners of the tesseroid. These two points efficiently define 
		 * dimensions of the tesseroid.
		 */
		vertex3ds *dl, *ur;
		/**
		 * neighbors of the tesseroid stored in the order longitudinal neighbor on left, 
		 * longitudinal neighbor on right, latitudinal neighbor in front, latitudinal neighbor in back, 
		 * radial neighbor in bottom and radial neighbor in top.
		 */
		type_tesseroid<A> *neigh[6];
		/**
		 * Constructor
		 */
		type_tesseroid();
		/**
		 * @brief      Constructs an object from a vertex array
		 *
		 * @param[in]  vert_arr  The vertex array
		 * @param[in]  n0_id     The vertex 0 index
		 * @param[in]  n1_id     The vertex 1 index
		 * @param[in]  n2_id     The vertex 2 index
		 * @param[in]  n3_id     The vertex 3 index
		 * @param[in]  n4_id     The vertex 4 index
		 * @param[in]  n5_id     The vertex 5 index
		 * @param[in]  n6_id     The vertex 6 index
		 * @param[in]  n7_id     The vertex 7 index
		 * @param[in]  index     The element index
		 */
		type_tesseroid(const array<vertex3ds> &vert_arr, int n0_id, int n1_id, int n2_id, int n3_id, 
			int n4_id, int n5_id, int n6_id, int n7_id, int index = 0);
		/**
		 * @brief      Construct a new tesseroid object from parameters
		 * 
		 * @warning    This function will locate memories to store vertice
		 *
		 * @param[in]  lonmin   Minimal coordinates of longitude
		 * @param[in]  lonmax   Maximal coordinates of longitude
		 * @param[in]  latmin   Minimal coordinates of latitude
		 * @param[in]  latmax   Maximal coordinates of latitude
		 * @param[in]  radmin   Minimal coordinates of radius
		 * @param[in]  radmax   Maximal coordinates of radius
		 * @param[in]  index  The element index
		 */
		type_tesseroid(double radmin, double radmax, double lonmin, double lonmax, 
			double latmin, double latmax, int index = 0);
		/**
		 * @brief      De-constructor
		 */
		virtual ~type_tesseroid(){}
		/**
		 * @brief      Constructs an object from a vertex array
		 *
		 * @param[in]  vert_arr  The vertex array
		 * @param[in]  n0_id     The vertex 0 index
		 * @param[in]  n1_id     The vertex 1 index
		 * @param[in]  n2_id     The vertex 2 index
		 * @param[in]  n3_id     The vertex 3 index
		 * @param[in]  n4_id     The vertex 4 index
		 * @param[in]  n5_id     The vertex 5 index
		 * @param[in]  n6_id     The vertex 6 index
		 * @param[in]  n7_id     The vertex 7 index
		 * @param[in]  index     The element index
		 */
		void set(const array<vertex3ds> &vert_arr, int n0_id, int n1_id, int n2_id, int n3_id, 
			int n4_id, int n5_id, int n6_id, int n7_id, int index = 0);
		/**
		 * @brief      Construct a new tesseroid object from parameters
		 * 
		 * @warning    This function will locate memories to store vertice
		 *
		 * @param[in]  lonmin   Minimal coordinates of longitude
		 * @param[in]  lonmax   Maximal coordinates of longitude
		 * @param[in]  latmin   Minimal coordinates of latitude
		 * @param[in]  latmax   Maximal coordinates of latitude
		 * @param[in]  radmin   Minimal coordinates of radius
		 * @param[in]  radmax   Maximal coordinates of radius
		 * @param[in]  index  The element index
		 */
		void set(double radmin, double radmax, double lonmin, double lonmax, 
			double latmin, double latmax, int index = 0);
		/**
         * @brief      Reset the structure to initial state
         */
        void reset();
	};

	template <typename A>
	type_tesseroid<A>::type_tesseroid() : entity<vertex3ds, 8, A>::entity()
	{
		dl = ur = nullptr;
		for (int i = 0; i < 6; ++i)
		{
			neigh[i] = nullptr;
		}
	}

	template <typename A>
	type_tesseroid<A>::type_tesseroid(const array<vertex3ds> &vert_arr, int n0_id, int n1_id, int n2_id, int n3_id, 
		int n4_id, int n5_id, int n6_id, int n7_id, int index) : type_tesseroid()
	{
		set(vert_arr, n0_id, n1_id, n2_id, n3_id, n4_id, n5_id, n6_id, n7_id, index);
	}

	template <typename A>
	type_tesseroid<A>::type_tesseroid(double radmin, double radmax, double lonmin, double lonmax, 
		double latmin, double latmax, int index) : type_tesseroid()
	{
		set(radmin, radmax, lonmin, lonmax, latmin, latmax, index);
	}

	template <typename A>
	void type_tesseroid<A>::set(const array<vertex3ds> &vert_arr, int n0_id, int n1_id, int n2_id, int n3_id, 
		int n4_id, int n5_id, int n6_id, int n7_id, int index)
	{
	    if (index < 0)
	    {
	        throw out_of_range("Invalid index number, From type_tesseroid::set(...)");
	    }

	    if (vert_arr.size() < 8)
	    {
	        throw length_error("Invalid array size, From type_tesseroid::set(...)");
	    }

	    if (vert_arr[n0_id].lon >= vert_arr[n6_id].lon || vert_arr[n0_id].lat >= vert_arr[n6_id].lat 
	    	|| vert_arr[n0_id].rad >= vert_arr[n6_id].rad)
	    {
	        throw invalid_argument("Invalid vertex's coordinates. From type_tesseroid::set(...)");
	    }

	    this->id = index;
		this->vert[0] = vert_arr.get(n0_id);
		this->vert[1] = vert_arr.get(n1_id);
		this->vert[2] = vert_arr.get(n2_id);
		this->vert[3] = vert_arr.get(n3_id);
		this->vert[4] = vert_arr.get(n4_id);
		this->vert[5] = vert_arr.get(n5_id);
		this->vert[6] = vert_arr.get(n6_id);
		this->vert[7] = vert_arr.get(n7_id);
	    dl = this->vert[0];
	    ur = this->vert[6];
	    return;
	}

	template <typename A>
	void type_tesseroid<A>::set(double radmin, double radmax, double lonmin, double lonmax, 
		double latmin, double latmax, int index)
	{
	    if (index < 0)
	    {
	        throw out_of_range("Invalid index number, From type_tesseroid::set(...)");
	    }

		if (lonmin >= lonmax || latmin >= latmax || radmin >= radmax)
		{
			throw invalid_argument("Invalid parameters. From type_tesseroid::set(...)");
		}

		for (int i = 0; i < 8; ++i)
		{
			this->vert[i] = new vertex3ds;
		}
		this->self_host = true;

		this->id = index;
		this->vert[0]->set(point3ds(radmin, lonmin, latmin), 0);
		this->vert[1]->set(point3ds(radmin, lonmax, latmin), 1);
		this->vert[2]->set(point3ds(radmin, lonmax, latmax), 2);
		this->vert[3]->set(point3ds(radmin, lonmin, latmax), 3);
		this->vert[4]->set(point3ds(radmax, lonmin, latmin), 4);
		this->vert[5]->set(point3ds(radmax, lonmax, latmin), 5);
		this->vert[6]->set(point3ds(radmax, lonmax, latmax), 6);
		this->vert[7]->set(point3ds(radmax, lonmin, latmax), 7);
		dl = this->vert[0];
		ur = this->vert[6];
		return;
	}

	template <typename A>
	void type_tesseroid<A>::reset()
	{
	    entity<vertex3ds, 8, A>::reset();
	    dl = ur = nullptr;
	    for (int i = 0; i < 6; ++i)
		{
			neigh[i] = nullptr;
		}
	    return;
	}

	template <typename A>
	void copy_type_tesseroid(type_tesseroid<A> *tar, const type_tesseroid<A> *src)
	{
		copy_entity(tar, src);

		tar->dl = src->dl;
		tar->ur = src->ur;

		for (size_t i = 0; i < 6; i++)
		{
			tar->neigh[i] = src->neigh[i];	
		}
		return;
	}

	template <typename A, typename B>
	void copy_type_tesseroid(type_tesseroid<A> *tar, const type_tesseroid<B> *src)
	{
		copy_entity(tar, src);

		tar->dl = src->dl;
		tar->ur = src->ur;
		return;
	}
}

#endif // _GCTL_TESSEROID_H